#include "mcu.h"

/*===================== GLOBAL VARIABLES DECLARATION ============================*/

uint32_t MCAL_Mcu_ClockGlobalStatus;
uint32_t MCAL_Mcu_LossOfLockCounter;
uint32_t MCAL_Mcu_BusFrequency;


/*======================== FUNCTION DEFINITIONS =================================*/

/*********************************************************************************
*	Name: Mcu_PllON       
*	Type: void                                                                    
*	Parameters: None                                                              
*	Description: Enables the PLL module and wait for the PLL to be configured                 
**********************************************************************************/

void Mcu_PllON(void)
{
    /* PLL enabled */
    PLLCTL_PLLON = 1u;                      
    /* Initialize PLL Loss-of-clock event counter */    
    MCAL_Mcu_LossOfLockCounter = 0u;            
    /* Wait until the PLL is within the desired frequency */
    while(!CRGFLG_LOCK)
    {}
    /* Initialize Global status variable to default value */                                   
    MCAL_Mcu_ClockGlobalStatus = MCU_STATUS_PLL_OK;        
    /* Clear IPLL Lock Interrupt Flag */
    CRGFLG_LOCKIF = 1u;                  
    /* Interrupt will be requested whenever LOCKIF(IPLL Lock Interrupt Flag)is set */        
    CRGINT_LOCKIE = 1u;                        
}

/*********************************************************************************
*	Name: Mcu_PllOFF       
*	Type: void                                                                    
*	Parameters: None                                                              
*	Description: Disable the PLL and change its status                 
**********************************************************************************/
void Mcu_PllOFF(void)
{
    /* Disable the PLL */
    PLLCTL_PLLON = 0u;
    /* update global status variable */                                   
    MCAL_Mcu_ClockGlobalStatus = MCU_STATUS_PLL_OFF;
}

/*********************************************************************************
*	Name: Mcu_SelectClockSourcePLL       
*	Type: void                                                                    
*	Parameters: None                                                              
*	Description: Selects PLL as the Clock Source                 
**********************************************************************************/

void Mcu_SelectClockSourcePLL(void)
{
    /* Select PLL as clock source */
    CLKSEL_PLLSEL = 1u;
    /* update global status variable */                                   
    MCAL_Mcu_ClockGlobalStatus = MCU_STATUS_PLL_SELECTED;
    /* update bus frequency variable with PLL frequency value */
    MCAL_Mcu_BusFrequency = (uint32_t)MCU_STATUS_ACTUAL_BUS_FREQ;
}

/*********************************************************************************
*	Name: Mcu_SelectClockSourceXTAL      
*	Type: void                                                                    
*	Parameters: None                                                              
*	Description: Selects XTAL as the Clock Source                 
**********************************************************************************/

void Mcu_SelectClockSourceXTAL(void)
{
    /* Select XTAL as clock source */
    CLKSEL_PLLSEL = 0u;
    /* update global status variable */                                   
    MCAL_Mcu_ClockGlobalStatus = MCU_STATUS_XTAL_SELECTED;
    /* update bus frequency variable with XTAL frequency value */
    MCAL_Mcu_BusFrequency = (uint32_t)(CNF_XTAL_FREQ_KHZ*1000u);
}

/*********************************************************************************
*	Name: Mcu_PllInit        
*	Type: void                                                                    
*	Parameters: None                                                              
*	Description: Init the System and PLL regs                 
**********************************************************************************/
void Mcu_PllInit(void)
{       
    /* In order to configure the PLL, first the PLL will be disable */
    Mcu_PllOFF();             
    /* Set PLL synthesizer register */
    SYNR_SYNDIV = SYNR_VALUE;         
    /* Set PLL divider register */  
    REFDV_REFDIV = REFDV_VALUE;         
    /* Configure PLL filter for optimal stability and lock time */
    REFDV_REFFRQ = REFFRQ_VALUE;        
    /* Set Postdiv value,  */
    POSTDIV_POSTDIV = POSTDIV_VALUE;                
    /* Configure VCO gain for optimal stability and lock time */
    SYNR_VCOFRQ = VCOFRQ_VALUE;         
    /* Clock Monitor Enable. This is required to enter Self Clock Mode */
    PLLCTL_CME = 1u;                     
    /* Self Clock Mode Enable, If CME & SCME are both asserted and the Clock Monitor
    in the oscillator block detects a loss of clock, Self Clock Mode will be entred */
    PLLCTL_SCME = 1u;                    
    /* Clear Self Clock Mode Interrupt Flag */                                                                          
    CRGFLG_SCMIF = 1u;                   
    /* Interrupt will be requested whenever SCMIF(Self Clock Mode Interrupt Flag) is set */ 
    CRGINT_SCMIE = 1u; 
    /* Enable PLL and wait for locking */
    Mcu_PllON();                   
}


/*********************************************************************************
*	Name: Mcu_Init                                                                
*	Type: void                                                                    
*	Parameters: None                                                              
*	Description: init several configuration as MMCV and PLL clock                 
**********************************************************************************/
void Mcu_Init(void){
	/*initialization for the PLL module*/
	Mcu_PllInit(); 
	
}

/*********************************************************************************
*	Name: Mcu_InitClock                                                              
*	Type: void                                                                    
*	Parameters: None                                                              
*	Description: This function will init the clock of PLL.              
**********************************************************************************/
void Mcu_InitClock(void){
    Mcu_SelectClockSourcePLL();
	
}

/*********************************************************************************
*	Name: Mcu_DistributePllClock        
*	Type: void                                                                    
*	Parameters: None                                                              
*	Description: init several configuration as MMCV and PLL clock                 
**********************************************************************************/
void Mcu_DistributePllClock(void){

	Mcu_SelectClockSourceXTAL();

	
}

